package sourceapi.client;

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.*;

public class SearchBox extends TextBox implements KeyUpHandler, KeyPressHandler, ValueChangeHandler<String>, KeyDownHandler {
	
	Timer DelayTimer;
	
	public SearchBox(){
		
		this.addKeyUpHandler( this );
		this.addKeyPressHandler( this );
		this.addValueChangeHandler( this );
		this.addKeyDownHandler( this );
		
		DelayTimer = new Timer() {
			public void run() {
				CallUpdateMenus();
			}
		};
		
	}
	
	private void CallDelayedUpdateMenus(){
		
		DelayTimer.cancel();
		DelayTimer.schedule( 100 );
		
	}
	
	private void CallUpdateMenus(){
		MenuManager.UpdateMenus();
	}

	public void onKeyUp(KeyUpEvent event) {
		this.CallDelayedUpdateMenus();	
	}

	public void onKeyPress(KeyPressEvent event) {
		this.CallDelayedUpdateMenus();	
	}

	public void onValueChange(ValueChangeEvent<String> event) {
		this.CallDelayedUpdateMenus();	
	}

	public void onKeyDown(KeyDownEvent event) {
		if( event.getNativeKeyCode() == KeyCodes.KEY_ESCAPE ){
			event.stopPropagation();
			this.setText( "" );
			CallUpdateMenus();
		}
	}

	
}
